﻿using IDAL;

namespace DAL
{
    public class BaseDAL<T> : IBaseDAL<T> where T : class
    {

        public BaseDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        public async Task<int> AddAsync(T entity)
        {
            await Context.Set<T>().AddAsync(entity);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> AddRangeAsync(List<T> entites)
        {
            await Context.Set<T>().AddRangeAsync(entites);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(T entity)
        {
            Context.Set<T>().Remove(entity);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DeleteRangeAsync(List<T> entites)
        {
            Context.Set<T>().RemoveRange(entites);
            return await Context.SaveChangesAsync();
        }

        public IQueryable<T> GetValues()
        {
            return Context.Set<T>();
        }

        public async Task<int> UpdateAsync(T entity)
        {
            Context.Set<T>().Update(entity);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> UpdateRangeAsync(List<T> entites)
        {
            Context.Set<T>().UpdateRange(entites);
            return await Context.SaveChangesAsync();
        }
    }
}
